package org.jsola.hr.service.excel;

import lombok.extern.slf4j.Slf4j;
import org.jsola.common.BeanKit;
import org.jsola.common.StrKit;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.CheckInPeopleConstant;
import org.jsola.hr.dto.CheckInPeopleAddDTO;
import org.jsola.hr.dto.excel.CheckInPeopleImportDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.ChannelQuery;
import org.jsola.hr.service.IChannelService;
import org.jsola.hr.service.ICheckInPeopleService;
import org.jsola.hr.vo.ChannelListVO;
import org.jsola.hr.vo.CheckInPeopleVO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author WU
 */
@Slf4j
public class CheckInPeopleImportListener extends BaseAnalysisEventListener<CheckInPeopleImportDTO> {

    /**
     * true忽略错误的行，false有错误中断上传
     */
    protected Boolean force = true;

    /**
     * 导入失败的预登记信息
     */
    protected List<CheckInPeopleVO> failCheckInPeopleList;

    /**
     * 导入成功的预登记信息
     */
    protected List<CheckInPeopleVO> successCheckInPeopleList = new ArrayList<>();


    /**
     * 入库
     */
    @Override
    public void saveData() {
        log.info("{}条数据，开始存储数据库！", dataList.size());
        if (dataList.size() == 0) {
            log.warn("没有数据需要入库");
            throw new ParamException("导入的数据为空，请检查模板中是否有内容");
        }
        for (int i = 0; i < dataList.size(); i++) {
            failCheckInPeopleList = new ArrayList<>();
            CheckInPeopleImportDTO checkInPeopleImportDTO = dataList.get(i);
            CheckInPeopleAddDTO checkInPeopleAddDTO = checkInPeopleImportDTO.to(CheckInPeopleAddDTO.class);
            HrException.notNull(tokenUser, "没有设置当前登录用户");
            ICheckInPeopleService checkInPeopleService = BeanKit.getBean(ICheckInPeopleService.class);
            // 根据渠道名称查询渠道信息
            if (StrKit.isNotEmpty(checkInPeopleImportDTO.getChannelName())) {
                IChannelService channelService = BeanKit.getBean(IChannelService.class);
                ChannelQuery channelQuery = new ChannelQuery();
                channelQuery.setName(checkInPeopleImportDTO.getChannelName());
                List<ChannelListVO> channelListVOList = channelService.select(channelQuery, tokenUser.getSiteId());
                ParamException.isFalse(CollectionUtils.isEmpty(channelListVOList),
                        "当前渠道名称中没有:"+checkInPeopleImportDTO.getChannelName()+"，请确保渠道中有该渠道");
                checkInPeopleAddDTO.setChannelId(channelListVOList.get(0).getId());
            }
            // 校验人员状态：如果非法状态则默认已接到
            checkInPeopleAddDTO.setSource("预登记");
            if (StrKit.isNotEmpty(checkInPeopleImportDTO.getStatusEnum())) {
                Integer status = "已接到".equals(checkInPeopleImportDTO.getStatusEnum()) ?
                        CheckInPeopleConstant.STATUS_ONE : "已安排".equals(checkInPeopleImportDTO.getStatusEnum()) ?
                        CheckInPeopleConstant.STATUS_TWO : "已面试".equals(checkInPeopleImportDTO.getStatusEnum()) ?
                        CheckInPeopleConstant.STATUS_THREE : CheckInPeopleConstant.STATUS_ONE;
                checkInPeopleAddDTO.setStatus(status);
            }
            CheckInPeopleVO checkInPeopleVO = checkInPeopleService.saveNoPE(checkInPeopleAddDTO, tokenUser);
            if (Objects.nonNull(checkInPeopleVO)) {
                successCount++;
                successCheckInPeopleList.add(checkInPeopleVO);
            } else {
                checkInPeopleVO = checkInPeopleImportDTO.to(CheckInPeopleVO.class);
                failCheckInPeopleList.add(checkInPeopleVO);
            }
        }
        log.info("存储数据库成功！");
    }

    public List<CheckInPeopleVO> getFailCheckInPeopleList() {
        return failCheckInPeopleList;
    }
}
